package edu.hut.bookshop.controller;

import edu.hut.bookshop.pojo.Book;
import edu.hut.bookshop.pojo.Comment;
import edu.hut.bookshop.pojo.User;
import edu.hut.bookshop.service.CommentService;
import edu.hut.bookshop.service.UserService;
import edu.hut.bookshop.util.ResultCode;
import edu.hut.bookshop.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * 评论的控制类
 */
@Controller
@RequestMapping("/com")
public class CommentController {
    @Autowired
    private CommentService commentService;

    @Autowired
    private UserService userService;

    @ResponseBody
    @GetMapping("/insert")
    public ResultVO insertCom(Integer jiajuId, String comment, HttpSession session){
        User user = userService.selectByUserId(2020002);
        Comment comment1 = new Comment();
        comment1.setJiajuId(jiajuId);
        comment1.setComment(comment);
        comment1.setUserId(user.getUserId());
        comment1.setUserName(user.getUserName());
        comment1.setCcreateTime(new Date());
        int rows = commentService.insertCom(comment1);
        return new ResultVO(ResultCode.SUCCESS,rows);
    }

    @ResponseBody
    @GetMapping("/selectbycId")
    public ResultVO selectBycId(Long cId){
        Comment comment = commentService.selectBycId(cId);
        return new ResultVO(ResultCode.SUCCESS,comment);
    }

    @ResponseBody
    @GetMapping("/selectbyjiajuId")
    public ResultVO selectByjiajuId(){
        Integer jiajuId = 1041;
        List<Comment> comments = commentService.selectByjiajuId(jiajuId);
        return new ResultVO(ResultCode.SUCCESS,comments);
    }

    @ResponseBody
    @GetMapping("/selectbyuserId")
    public ResultVO selectByuserId(Integer userId){
        List<Comment> comments = commentService.selectcomByuserId(userId);
        return new ResultVO(ResultCode.SUCCESS,comments);
    }

    @GetMapping("/comment")
    public String comments(Model model,Integer jiajuId){
        List<Comment> comments = commentService.findcomment(jiajuId);
        model.addAttribute("comments",comments);
        return "comment :: commentList";
    }


    @PostMapping("/comment")
    public ResultVO post(@RequestParam String parentId,@RequestParam String comments, @RequestParam Integer userId,
                         @RequestParam Integer jiajuId){
        Comment comment = new Comment();
        if(parentId!=null){
            comment.setParentId(Long.parseLong(parentId));
        }
        //System.out.println(comment.getParentId());
        comment.setUserId(userId);
        comment.setJiajuId(jiajuId);
        comment.setComment(comments);

        commentService.insertCom(comment);
        return new ResultVO(ResultCode.SUCCESS,"redirect:/comment");
    }
    @GetMapping("/tocom")
    public String tocom(){
        return "comment";
    }
}
